package com.yoomiito.app.ui.welcome;

import android.content.Intent;
import android.os.Bundle;
import androidx.viewpager.widget.ViewPager;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;

import com.jakewharton.rxbinding3.view.RxView;
import com.yoomiito.app.adapter.VPAdapter;
import com.yoomiito.app.base.App;
import com.yoomiito.app.base.BaseActivity;
import com.yoomiito.app.Constant;
import com.yoomiito.app.MainActivity;
import com.yoomiito.app.R;
import com.yoomiito.app.model.config.SystemInfo;
import com.yoomiito.app.net.YmApi;
import com.yoomiito.app.utils.L;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import cn.droidlover.xdroidmvp.cache.SharedPref;
import cn.droidlover.xdroidmvp.net.ApiException;
import cn.droidlover.xdroidmvp.net.ApiSubscriber;
import cn.droidlover.xdroidmvp.net.XApi;

public class SplashPagerActivity extends BaseActivity {


    @BindView(R.id.bannerView)
    ViewPager bannerView;
    @BindView(R.id.btn_go)
    Button btnGo;

    private List<View> pics;
    private int[] res = new int[]{R.drawable.guid_1,R.drawable.guid_2,R.drawable.guid_3};
    private String mPushMsg;

    @Override
    public void initData(Bundle savedInstanceState) {
        L.i("进入Splash");
        Intent intent = getIntent();
        if (null!=intent){
            mPushMsg = getIntent().getStringExtra(Constant.UM_PUSH_MESSAGE);
            L.i("透传推送消息："+mPushMsg);
        }
        SharedPref.getInstance(App.getInstance()).putBoolean(Constant.IS_FIRST,true);
        pics = new ArrayList<>();
        for (int i:res){
            ImageView view = new ImageView(this);
            view.setScaleType(ImageView.ScaleType.FIT_XY);
            ViewPager.LayoutParams lp = new ViewPager.LayoutParams();
            lp.width = ViewPager.LayoutParams.MATCH_PARENT;
            lp.height = ViewPager.LayoutParams.MATCH_PARENT;
            view.setLayoutParams(lp);
            view.setBackgroundResource(i);
            pics.add(view);
        }
        bannerView.setAdapter(new VPAdapter(pics));
        bannerView.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
                if (position==pics.size()-1){
//                    bannerView.setOnTouchListener(new View.OnTouchListener() {
//                        @Override
//                        public boolean onTouch(View v, MotionEvent event) {
//                            switch (event.getAction()){
//                                case MotionEvent.ACTION_DOWN:
//                                    x = (int) event.getX();
//                                    break;
//                                case MotionEvent.ACTION_MOVE:
//                                    int currentX = (int) event.getX();
//                                    if (Math.abs(x-currentX)>100){
//                                        onViewClicked();
//                                    }
//                                    break;
//                            }
//                            return false;
//                        }
//                    });
                    btnGo.setVisibility(View.VISIBLE);
                }else {
                    if (View.VISIBLE==btnGo.getVisibility()){
                        btnGo.setVisibility(View.GONE);
                    }
                }
            }

            @Override
            public void onPageSelected(int position) {

            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });
        RxView.clicks(btnGo).subscribe(unit -> {
            getSystemConfig();

        });
    }

    public void getSystemConfig() {
        Intent intent_1 = new Intent(SplashPagerActivity.this,MainActivity.class);
        if (!TextUtils.isEmpty(mPushMsg)){//不为空 则表示有离线推送消息透传过来  如果有离线推送消息透传过来 则一直传递到MainActivity
            intent_1.putExtra(Constant.UM_PUSH_MESSAGE,mPushMsg);
        }
        startActivity(intent_1);
        SplashPagerActivity.this.finish();



//        showLoading();
//        YmApi.getYmService()
//                .getSystemInfo(1)
//                .compose(bindToLifecycle())
//                .compose(XApi.getApiTransformerData())
//                .compose(XApi.getScheduler())
//                .subscribe(new ApiSubscriber<SystemInfo>() {
//                    @Override
//                    public void onSuccess(SystemInfo systemInfo) {
//                            dismissLoading();
//                            L.i("ss:"+TextUtils.isEmpty(systemInfo.getTime())+"  "+ TextUtils.isEmpty(systemInfo.getTitle()) +"   "+TextUtils.isEmpty(systemInfo.getVice_title()));
//                            if (systemInfo == null ||
//                                    (TextUtils.isEmpty(systemInfo.getTime()) &&
//                                            TextUtils.isEmpty(systemInfo.getTitle()) &&
//                                            TextUtils.isEmpty(systemInfo.getVice_title()))) {
//
//                            }else {
//                                showSystemInfo(systemInfo);
//                            }
//                        }
//
//                    @Override
//                    public void onFail(ApiException error) {
//                            L.i("SystemInfo Error:" + error.getCode() + "  " + error.getMsg());
//                    }
//                });
    }

    public void showSystemInfo(SystemInfo systemInfo) {
        SystemInfoActivity.Companion.start(this, systemInfo);
        finish();
    }

    @Override
    public int getLayoutId() {
        return R.layout.activity_splash_pager;
    }

    @Override
    public Object newP() {
        return null;
    }

}
